package com.fingard.dsp.bank.directbank.hxb02;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.hxb02.util.SocketClient;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

public class HXB02Base extends DirectBase {
	protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }
    
	protected void writeCommonHeader(XmlTextWriter xmlWriter, HXB02Header tmpBankHeader){
		xmlWriter.writeStartDocument();
		xmlWriter.writeStartElement("HXE");
        xmlWriter.writeStartElement("Head");
        xmlWriter.writeElementString("Identification", tmpBankHeader.reqSeqNo);
        xmlWriter.writeElementString("TrnxCode", tmpBankHeader.transCode);
        xmlWriter.writeElementString("TrnxDatetime", tmpBankHeader.transTime);
        xmlWriter.writeEndElement();
    }
	
	public String sign(String message, String charset) throws Exception{ 
		String socketip = bankFront.serverIP;
		String signport = bankFront.signPort;
		WriteBankLogLn2("签名服务器地址:'" + socketip + ":" + signport + "'");
		StringBuffer sb = new StringBuffer();
		String newline = "\r\n";
		sb.append("POST HTTP://"+socketip+"/servlet/ABC95599.Trans HTTP/1.0");
		sb.append(newline);
		sb.append("Accept:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*");
		sb.append(newline);
		sb.append("Referer: HTTP://"+socketip+"/personRACrtLY.htm");
		sb.append(newline);
		sb.append("Accept-Language: zh-cn");
		sb.append(newline);
		sb.append("Content-Type: INFOSEC_SIGN/1.0");
		sb.append(newline);
		sb.append("Proxy-Connection: Keep-Alive");
		sb.append(newline);
		sb.append("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
		sb.append(newline);
		sb.append("Host: "+socketip+":"+signport);
		sb.append(newline);
		sb.append("Content-Length: " + message.getBytes(charset).length);
		sb.append(newline);
		sb.append("Pragma: no-cache");
		sb.append(newline);
		sb.append(newline);
		sb.append(message);
		WriteBankLogLn2("签名请求数据:\r\n" + sb.toString());
		
		String tmpStrRet = new String(SocketClient.sendInfo(sb.toString().getBytes(charset), 120*1000, socketip, Integer.parseInt(signport)));
//		String tmpStrRet = FileHelper.readFileText("E:/test/sign.txt", charset);
		WriteBankLogLn2("签名响应数据:\r\n" + tmpStrRet);
		if (StringHelper.hasAnyChar(tmpStrRet) && tmpStrRet.indexOf("<") > 0) {
			tmpStrRet = tmpStrRet.substring(tmpStrRet.indexOf("<"));
		}
		
		return tmpStrRet;
	}
	
	public String verifysign(String signMessage, String charset) throws Exception{
		String socketip = bankFront.serverIP;
		String signport = bankFront.signPort;
		WriteBankLogLn2("验签服务器地址:'" + socketip + ":" + signport + "'");
		
		String newline = "\r\n";
		StringBuffer sb = new StringBuffer();
		sb.append("POST HTTP://"+socketip+"/servlet/ABC95599.Trans HTTP/1.0");
		sb.append(newline);
		sb.append("Accept:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*");
		sb.append(newline);
		sb.append("Referer: HTTP://"+socketip+"/personRACrtLY.htm");
		sb.append(newline);
		sb.append("Accept-Language: zh-cn");
		sb.append(newline);
		sb.append("Content-Type: INFOSEC_VERIFY_SIGN/1.0");
		sb.append(newline);
		sb.append("Proxy-Connection: Keep-Alive");
		sb.append(newline);
		sb.append("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
		sb.append(newline);
		sb.append("Host: "+socketip+":"+signport);
		sb.append(newline);
		sb.append("Content-Length: " + signMessage.getBytes(charset).length);
		sb.append(newline);
		sb.append("Pragma: no-cache");
		sb.append(newline);
		sb.append(newline);
		sb.append(signMessage);
		WriteBankLogLn2("验签原始请求数据:\r\n" + sb.toString());
		
		String tmpStrRet = new String(SocketClient.sendInfo(sb.toString().getBytes(), 120*1000, socketip, Integer.parseInt(signport)));
//		String tmpStrRet = FileHelper.readFileText("E:/test/9108.txt", charset);
//		String tmpStrRet = FileHelper.readFileText("E:/test/9144.txt", charset);
		WriteBankLogLn2("验签响应数据:\r\n" + tmpStrRet);
		if (StringHelper.hasAnyChar(tmpStrRet) && tmpStrRet.indexOf("<") > 0) {
			tmpStrRet = tmpStrRet.substring(tmpStrRet.indexOf("<"));
		}
		
		return tmpStrRet;
	}
	
	public String sendToBank(String message, String charset) throws Exception{
		String socketip = bankFront.serverIP;
		String socketport = bankFront.serverPort;
		String signport = bankFront.signPort;
		String url = bankFront.serverURL;
		WriteBankLogLn2("交易服务器地址:'" + socketip + ":" + socketport + "'");
		String newline = "\r\n";
		StringBuffer sb = new StringBuffer();
		sb.append("POST "+url+" HTTP/1.0");
		sb.append(newline);
		sb.append("Accept:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*");
		sb.append(newline);
		sb.append("Referer: HTTP://"+socketip+"/personRACrtLY.htm");
		sb.append(newline);
		sb.append("Accept-Language: zh-cn");
		sb.append(newline);
		sb.append("Content-Type: INFOSEC_VERIFY_SIGN/1.0");
		sb.append(newline);
		sb.append("Proxy-Connection: Keep-Alive");
		sb.append(newline);
		sb.append("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
		sb.append(newline);
		sb.append("Host: "+socketip+":"+signport);
		sb.append(newline);
		sb.append("Content-Length: " + message.getBytes(charset).length);
		sb.append(newline);
		sb.append("Pragma: no-cache");
		sb.append(newline);
		sb.append(newline);
		sb.append(message);
		WriteBankLogLn2("交易请求数据:\r\n" + sb.toString());
		
		String tmpStrRet = new String(SocketClient.sendInfo(sb.toString().getBytes(), 30*1000, socketip, Integer.parseInt(socketport)));
//		String tmpStrRet = FileHelper.readFileText("E:/test/resp.txt", charset);
		WriteBankLogLn2("交易响应数据:\r\n" + tmpStrRet);
		
		String[] str2 = tmpStrRet.split("\r\n\r\n");
		if(str2 != null && str2.length >1){
			if(str2[0].indexOf("200 OK") >= 0){
				tmpStrRet = str2[1];
			}else{
				throw new Exception("请求失败，请联系管理人员!");
			}
		}else{
			throw new Exception("请求失败，请联系管理人员!");
		}
		
		return tmpStrRet;
	}
}
